using System;
using System.Collections.Generic;
using System.Text;
using Dominio;
using NHibernate.Cfg;
using NHibernate;
using Exeptions;

namespace DAP
{
    public class UserRepository<T> : AbstractRepository
    {
        public UserRepository() : base()
        {
      
        }

        public T getUserByEmailAndPass(String email, String pass)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            IQuery query = sesion.CreateQuery("from User where Email = :x and Password = :y");
            query.SetString("x", email);
            query.SetString("y", pass);
            transaction.Commit();
            T result = (T)query.UniqueResult();

            return result;
        }
        public T getUserById(int id)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            IQuery query = sesion.CreateQuery("from User where userId = :x");
            query.SetString("x", id.ToString());
            transaction.Commit();
            T result = (T)query.UniqueResult();

            return result;
        }


        public void save(T user)
        {
            ISession sesion = sesiones.OpenSession();
            ITransaction transaction = sesion.BeginTransaction();
            try {
                sesion.SaveOrUpdate(user);
                transaction.Commit();

            }
            catch (Exception ex){

                transaction.Rollback();
                throw new HibernateException();
            }
        }
    }
}
